package com.rxyl.love.petdoctor.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.rxyl.love.petdoctor.app.MyApplication;
import com.rxyl.love.petdoctor.entity.Banner;
import com.rxyl.love.petdoctor.acticity.MainWebActivity;

import java.util.List;

/**
 * Created by jerry on 15/11/2.
 */
public class BannerListAdapter<T> extends PagerAdapter {
    private List<T> mBanners;
    private Context mContext;

    public BannerListAdapter(Context mContext, List<T> mBanners) {
        this.mBanners = mBanners;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mBanners.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        T bannerList = mBanners.get(position);

        ImageView imageView = new ImageView(mContext);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);

        String imageUrl = null;

        // 首页的图片
        if (bannerList instanceof Banner.DataEntity) {
            final Banner.DataEntity
                    banner = (Banner.DataEntity) bannerList;

            imageUrl = banner.getImage();

            // 使用UIL加载图片
            MyApplication.getApp()
                    .getImageLoader()
                    .displayImage(imageUrl, imageView,
                            MyApplication.getApp().getImageOptions());

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, MainWebActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("mainUrl",banner.getUrl());
                    intent.putExtra("lable","文章详情");
                    mContext.startActivity(intent);
                }
            });

            container.addView(imageView);

        }

        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
}
